//
//  RootViewController.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/9.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit
private let identifier : String = "cell"
class RootViewController: UIViewController {

    /// 添加TableView视图
    lazy var tableView : UITableView = {
       let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: identifier)
        return tableView
    }()
    
    /// 登录状态
    var isLogin : Bool {
        return  UserAccount.shareUserAccount.isLogin
    }
    
    /// 访客视图
    var visitorView : VisitorView?
    
    lazy var refreshCotroller : UIRefreshControl = {
        let refreshController = UIRefreshControl()
        refreshController.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        return refreshController
    }()
    
    lazy var indicator : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        setupUI()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSuccess:", name: loginSuccessNotification, object: nil)
    }
    
    override func didReceiveMemoryWarning() {
        
    }
    
    func loadData()
    {
        
    }

}
// MARK: - 设置UI控件
extension RootViewController : VisitorViewDelegate
{
    func setupUI()
    {
        setupTableView()
        if !isLogin
        {
            setupVisitorView()
            
        }
    }
    /**
     添加TableView主视图及其子控件
     */
    func setupTableView()
    {
        tableView.frame = screenBounds
        
        view .addSubview(self.tableView)
        
        tableView.addSubview(refreshCotroller)
        tableView.tableFooterView = indicator
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "signUp")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "login")
        
    }
    /**
     设置访客视图
     */
    func setupVisitorView()
    {
        visitorView = VisitorView()
        visitorView?.delegate = self 
        visitorView?.frame = screenBounds
        view.addSubview(visitorView!)
    
    }
    func click()
    {
        signUp()
    }
    /**
     导航栏注册按钮点击事件
     */
    func signUp()
    {
        let oAuthViewController = OAuthViewController()
        let navigationController = UINavigationController(rootViewController: oAuthViewController)
        presentViewController(navigationController, animated: true, completion: nil)
    }
    
    /**
     导航栏登录按钮点击事件
     */
    func login()
    {
        let oAuthViewController = OAuthViewController()
        let navigationController = UINavigationController(rootViewController: oAuthViewController)
        presentViewController(navigationController, animated: true, completion: nil)
    }
    /**
     登录成功
     */
    func loginSuccess(notification : NSNotification)
    {
        visitorView?.removeFromSuperview()
        navigationItem.leftBarButtonItem = nil
        navigationItem.rightBarButtonItem = nil
        loadData()
    }
    
}
//MARK: - TableViewDataSource & Delegate

extension RootViewController : UITableViewDelegate
{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }
}
extension RootViewController : UITableViewDataSource
{
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
        
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
        
        
        return cell
    }
}